package com.jiazhong.mingxing.boot.boot02.util;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@Component
public class NowDateHandler implements MetaObjectHandler {

    // 当执行添加操作时 自动填充录入的值
    public void insertFill(MetaObject metaObject) {
        log.info("用户执行了添加操作，开始自动填充");
        this.strictInsertFill(metaObject, "createTime", this::getNowDate, String.class);
    }

    // 当执行修改操作时 自动填充录入的值
    public void updateFill(MetaObject metaObject) {
        log.info("用户执行了修改操作，开始自动填充");
        this.strictUpdateFill(metaObject, "updateTime", this::getNowDate, String.class);

    }

    private String getNowDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
}
